﻿IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'MarcaSave')
	BEGIN
		DROP  Procedure  MarcaSave
	END
GO

CREATE PROCEDURE [dbo].[MarcaSave]
	(	
		@id			AS INT,
		@name		AS VARCHAR(50),
		@companyId	AS INT,
		@user		AS VARCHAR(30)
	)	
AS
BEGIN	
	SET NOCOUNT ON

	DECLARE @currentDate AS DATETIME
	SET @currentDate = GETDATE()
	
	IF EXISTS(SELECT 1 FROM Marca WHERE MarcaId = @id)
	BEGIN
		UPDATE Marca
		SET Nombre = @name,
			Activo = 1,
			FechaModificacion = @currentDate,
			ModificadoPor = @user		
		WHERE MarcaId = @id
	END
	ELSE
	BEGIN	
		SELECT @id = ISNULL(MAX(MarcaId),0)+1 FROM Marca

		INSERT INTO Marca
			(MarcaId, Nombre, Activo, FechaCreacion, FechaModificacion, ModificadoPor, EmpresaId)
		VALUES
			(@id, @name, 1, @currentDate, @currentDate, @user, @companyId)	
	END
	
	SET NOCOUNT OFF
END